package com.lys.lock;

import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestLock {
//    private ZooKeeper zooKeeper;
//
//    @Before
//    public void before(){
//        zooKeeper = ZookeeperUtil.getZooKeeper();
//    }
//
//    @After
//    public void after(){
//        try {
//            zooKeeper.close();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }

    @Test
    public void run(){
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                // 获取锁
                LockUtil lockUtil = new LockUtil();
                lockUtil.setZooKeeper(ZookeeperUtil.getZooKeeper());
                lockUtil.tryLock();
                // 业务逻辑
                String name = Thread.currentThread().getName();
                System.out.println(name+"工作中。。。。。。");
                // 释放锁
                lockUtil.reLock();
            }).start();
        }
        while(true){
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
